import pygame, sys, math, random

class Player():
    def __init__(self, speed = [2,2], size = [100,100], pos = (0,0), angle = 1):
        upImages = ["rsc/player/move1.png",
                    "rsc/player/move2.png",
                    "rsc/player/move3.png"]
        self.upImages = []
        self.downImages = []
        self.rightImages = []
        self.leftImages = []
        for upImage in upImages:
            newimage = pygame.image.load(upImage)
            newimage = pygame.transform.scale(newimage, size)
            self.upImages += [newimage]
        for downImage in self.upImages:
            newimage = pygame.transform.flip(downImage, 0, 1)
            self.downImages += [newimage]
        for rightImage in self.upImages:
            newimage = pygame.transform.rotate(rightImage, -90)
            self.rightImages += [newimage]
        for leftImage in self.upImages:
            newimage = pygame.transform.rotate(leftImage, 90)
            self.leftImages += [newimage]
        self.images = self.upImages
        self.frame = 0
        self.maxFrame = len(self.images)-1
        self.waitCount = 0
        self.waitMax = 10
        self.image = self.images[self.frame]
        self.rect = self.image.get_rect()
        self.maxSpeedx = speed[0]
        self.maxSpeedy = speed[1]
        self.speedx = 0
        self.speedy = 0
        self.speed = [self.speedx, self.speedy]
        self.radius = self.rect.width/2
        self.place(pos)
        self.dir = "stop up"
        self.angle = 90
        
    def animate(self):
        if self.dir == "stop up":
            self.images = self.upImages
            self.frame = 0
            self.waitCount = 0
        elif self.dir == "up":
            self.images = self.upImages
            if self.waitCount < self.waitMax:
                self.waitCount += 1
            else: 
                self.waitCount = 0
                if self.frame < self.maxFrame:
                    self.frame += 1
                else:
                    self.frame = 0
        elif self.dir == "stop down":
            self.images = self.downImages
            self.frame = 0
            self.waitCount = 0
        elif self.dir == "down":
            self.images = self.downImages
            if self.waitCount < self.waitMax:
                self.waitCount += 1
            else: 
                self.waitCount = 0
                if self.frame < self.maxFrame:
                    self.frame += 1
                else:
                    self.frame = 0
        elif self.dir == "stop right":
            self.images = self.rightImages
            self.frame = 0
            self.waitCount = 0
        elif self.dir == "right":
            self.images = self.rightImages
            if self.waitCount < self.waitMax:
                self.waitCount += 1
            else: 
                self.waitCount = 0
                if self.frame < self.maxFrame:
                    self.frame += 1
                else:
                    self.frame = 0
        elif self.dir == "stop left":
            self.images = self.leftImages
            self.frame = 0
            self.waitCount = 0
        elif self.dir == "left":
            self.images = self.leftImages
            if self.waitCount < self.waitMax:
                self.waitCount += 1
            else: 
                self.waitCount = 0
                if self.frame < self.maxFrame:
                    self.frame += 1
                else:
                    self.frame = 0
        self.image = self.images[self.frame]


           
            
    def collideWall(self, width, height):
        #print "wall collide", width, height
        if self.rect.left < 0 or self.rect.right > width:
           self.speedx = 0
           #print"hit x", self.rect.left, self.rect.right
            
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speedy = 0
            #print"hit y", self.rect.top, self.rect.bottom
    
    def colidewallsegment(self,wall):
        if self.rect.right > wall.rect.left and self.rect.left < wall.rect.right:
            if self.rect.bottom > wall.rect.top and self.rect.top < wall.rect.bottom:
                self.speedx = 0
                self.speedy = 0
    
    def place(self, pos):
        self.rect.center = pos
        
    def direction(self, dir):
        self.dir = dir
        if dir == "right":
            self.speedx = self.maxSpeedx
        if dir == "stop right":
            self.speedx = 0
        if dir == "left":
            self.speedx = -self.maxSpeedx
        if dir == "stop left":
            self.speedx = 0
        if dir == "up":
            self.speedy = -self.maxSpeedy
        if dir == "stop up":
            self.speedy = 0
        if dir == "down":
            self.speedy = self.maxSpeedy
        if dir == "stop down":
            self.speedy = 0
        print self.speedx, self.speedy
        
        if dir == "right":
            self.angle = 0  
        if dir == "left":
            self.angle = 180
        if dir == "up":
            self.angle = 90
        if dir == "down":
            self.angle = 270
                
        
    def move(self):
        self.speed = [self.speedx, self.speedy]
        self.rect = self.rect.move(self.speed)
    
    def update(self):
        self.animate()
        self.move()
       